package com.movieknight;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import com.github.savvasdalkitsis.jtmdb.Country;
import com.github.savvasdalkitsis.jtmdb.Movie;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

// AddMovieInfoPageActivty shows the information about a movie.
// It gives the user the ability to add the movie to their collection or watch a Trailer.

public class AddMovieInfoPageActivity extends Activity {
	private int movieID;
	@SuppressWarnings("unused")
	private static final SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
	@SuppressWarnings("unused")
	private static final DateFormat dfLocal = DateFormat.getDateInstance();

	int getMovieID() {
		return movieID;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Set up view and start progress bar
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.movie);
		setProgressBarIndeterminateVisibility(true);
		
		// get the movie id and then the movie
		Bundle extras = getIntent().getExtras();
		movieID = extras.getInt("movieID");
		setFields(movieID);
		
		// stop the progress bar
		setProgressBarIndeterminateVisibility(false);
	}

	private void setFields(int movieID) {
		// TODO: replace by get movie by id... at the moment, we get it by list
		// position from most popular
		final Movie movie = TMDBMovieDownloader.get(movieID);

		// collect fields for primary information
		TextView movieName = (TextView) findViewById(R.id.movieName);
		ImageView moviePoster = (ImageView) findViewById(R.id.moviePoster);
		TextView movieAlternativeName = (TextView) findViewById(R.id.movieAlternativeName);
		TextView movieRelease = (TextView) findViewById(R.id.movieRelease);
		TextView movieOverview = (TextView) findViewById(R.id.movieOverview);
		TextView movieCountry = (TextView) findViewById(R.id.movieCountry);
		TextView movieLanguage = (TextView) findViewById(R.id.movieLanguage);
		Button movieTrailer = (Button) findViewById(R.id.movieTrailer);
		Button movieChoose = (Button) findViewById(R.id.movieChoose);

		movieName.setText(movie.getName());

		if (movie.getImages().posters != null
				&& movie.getImages().posters.iterator().hasNext()) {
			String url = movie.getImages().posters.iterator().next()
					.getSmallestImage().toString();
			Log.d("ShowMovieActivity", url);
			MovieKnightApp.imageDownloader.download(url, moviePoster);
		} else {
			moviePoster.setVisibility(View.GONE);
		}
		
		MovieKnightApp app = (MovieKnightApp) getApplication();
		String secondary = app.chooseSecondaryName(movie, true);
		
		if (secondary.equals("null")) {
			movieAlternativeName.setVisibility(View.GONE);
		} else {
			movieAlternativeName.setText(secondary);
		}


		if (movie.getReleasedDate() != null) {
			movieRelease.setText(getString(R.string.movieRelease) + " " + android.text.format.DateFormat.getDateFormat(getApplicationContext()).format(movie.getReleasedDate()));
			this.setTitle(movie.getName() + " (" + MovieKnightApp.sdfYear.format(movie.getReleasedDate()) + ")");
		} else {
			movieRelease.setVisibility(View.GONE);
			this.setTitle(movie.getName());
		}

		if (movie.getLanguage() != null && movie.getLanguage().length() > 0) {
			Locale locale = new Locale(movie.getLanguage());
			movieLanguage.setText(locale.getDisplayLanguage());
		} else {
			movieLanguage.setVisibility(View.GONE);
			findViewById(R.id.movieLanguageTitle).setVisibility(View.GONE);
		}

		if (movie.getCountries() != null
				&& movie.getCountries().iterator().hasNext()) {
			String countries = new String();
			Locale locale;

			for (Country country : movie.getCountries()) {
				locale = new Locale(Locale.getDefault().getLanguage(), country
						.getCode());
				countries += locale.getDisplayCountry() + ", ";
				Log.d("ShowMovieActivity", country.getCode() + "->"
						+ locale.getDisplayCountry());

			}
			// cut off the last coma and space when setting the countries
			movieCountry.setText(countries.subSequence(0,
					countries.length() - 2));
		} else {
			movieCountry.setVisibility(View.GONE);
			findViewById(R.id.movieCountryTitle).setVisibility(View.GONE);
		}

		if (movie.getOverview() != null && !movie.getOverview().equals("null")) {
			movieOverview.setText(movie.getOverview());
		} else {
			movieOverview.setVisibility(View.GONE);
			findViewById(R.id.movieOverviewTitle).setVisibility(View.GONE);
		}

		if (movie.getTrailer() != null) {
			movieTrailer.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					final String url = movie.getTrailer().toString();
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
					}
			});
		} else {
			movieTrailer.setVisibility(View.GONE);
			findViewById(R.id.movieTrailerTitle).setVisibility(View.GONE);
		}
		
		movieChoose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				// Prepare data intent 
				Intent data = new Intent();
				data.putExtra("returnKey1", getMovieID());
				// Activity finished okay, return the data
				setResult(RESULT_OK, data);
					finish();
			}
		});
	}

}
